﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

namespace SharpCmd.Data
{
    static class Configuration
    {
        public static string GetDirectory()
        {
            FileInfo di = new FileInfo(Assembly.GetExecutingAssembly().Location);
            return di.Directory.FullName;
        }

        public static bool SerializeType(object item, string filename)
        {
            try
            {
                System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                using (FileStream fs = new FileStream(filename, FileMode.Create))
                {
                    bf.Serialize(fs, item);
                    fs.Close();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static T DeserializeItem<T>(string filename) where T : class
        {
            try
            {
                BinaryFormatter bf = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                using (FileStream fs = new FileStream(filename, FileMode.Open))
                {
                    object o = bf.Deserialize(fs);
                    return (T)o;
                }
            }
            catch
            {
                return null;
            }
        }
    }
}
